package org.webball.pitch.phase;

import org.webball.base.abstracts.AbstractBall;
import org.webball.pitch.Pitch;
import org.webball.pitch.PitchConstants;

import org.webball.player.Player;
import org.webball.pitch.Team;
import org.webball.tools.CyclesCounter;
import org.webball.tools.MKVector;

/**
 * 
 * @author Maciej Koch
 *
 */
public class PlayPhase extends BasicPhase{

	//teams rebounds
	int[] rebounds;
	CyclesCounter positionControllerCounter = new CyclesCounter(50);
	//who last touched the ball
	int lastHit;
	boolean serve = true;
	
	public PlayPhase(Team[] teams, AbstractBall ball, Pitch pitch) {
		super(teams, ball, pitch);
		end = false;
		rebounds = new int[2];
		rebounds[Team.T1] = 0;
		rebounds[Team.T2] = 0;
	}
	@Override
	public void update() {
		//-------updates-------------------
		playersController();
		//ball
		ballController();
		//----------------------------------
		
		//---------ball hitting--------------		
		hit: for(int t = 0 ; t < 2 ; t++)
			for(int i = 0 ; i < teams[t].getAllPlayers().length ; i++) {
				Player player = (Player) teams[t].getPlayer(i);
				if(teams[t].getHittingPlayer() != i)//cant hit 2 times
					if(player.getPosition().distance(ball.getPosition()) <= (ball.getSize() + player.getSize())/2) {
						if(player.getHitTarget() != null)
							/*if(heightController(ball))*/{
								action(t, player, ball);
								lastHit = t;
								teams[t].setHittingPlayer(i);
								actionController(t);
								break hit;
						}
					}
		}
		//-----------------------------------
		setAction();
		
//		//temporary-----------------------------------
//		int i = 4;
//		MKVector old = teams[1].getPlayer(i).getPosition();
//		teams[Team.T2].getPlayer(i).setHitTarget(new MKVector(200,150).add(PitchConstants.firstStart));
//		if(ball.getMovementTarget().x > PitchConstants.half)
//			teams[Team.T2].getPlayer(i).setMovementTarget(ball.getMovementTarget());
//		else
//			teams[Team.T2].getPlayer(i).setMovementTarget(old);
//		//---------------------------------------------		
	}

	
	//------------------------------
	//---------private--------------
	//------------------------------
	private boolean heightController(AbstractBall ball) {
		int maxHeight = 20;
		float height = ball.getHeight();
		if(height > 0 && height < maxHeight)
			return true;
		return false;
	}
	private void actionController(int team) {
		
		rebounds[team]++;
		if(rebounds[team] > 3) {
			losePoint(team);
		}
	}
	private void losePoint(int team) {
		int at = Team.T1;
		if(team == Team.T1) at = Team.T2;
			
		teams[at].addPoint();
		lastAttackingTeam = attackingTeam;
		attackingTeam = at;
		stop();
	}
	private void setAction() {
		//right half
		if(ball.getPosition().x > PitchConstants.half) {
			rebounds[Team.T1] = 0;
			teams[Team.T1].setHittingPlayer(-1);
		}
		//left half
		else {
			rebounds[Team.T2] = 0;
			teams[Team.T2].setHittingPlayer(-1);
		}
	}
	private void playersController() {
		//players
		for(int i = 0 ; i < 6 ; i++) {
			teams[Team.T1].getPlayer(i).update();
			teams[Team.T2].getPlayer(i).update();
		}
		
		if(positionControllerCounter.check()) {
			for(int i = 0 ; i < 6 ; i++) {
				if(teams[Team.T1].getPlayer(i).getPosition().x > PitchConstants.half)
					losePoint(Team.T1);
				if(teams[Team.T2].getPlayer(i).getPosition().x < PitchConstants.half)
					losePoint(Team.T2);
			}
		}
	}
	private void ballController() {
		ball.update();
		if(ball.getHeight() == 0) {
			if(isOnHalf(Team.T1, ball.getPosition())) losePoint(Team.T1);
			else if(isOnHalf(Team.T2, ball.getPosition())) losePoint(Team.T2);
			else {
				if(lastHit == Team.T1) losePoint(Team.T1);
				else losePoint(Team.T2);
			}		
		}
		
			
	}
	private boolean isOnHalf(int h, MKVector pos){
		MKVector start = null;
		MKVector end = null;
		if(h == Team.T1) {
			start = PitchConstants.firstStart;
			end = PitchConstants.firstEnd;		
		}
		if(h == Team.T2) {
			start = PitchConstants.secondStart;
			end = PitchConstants.secondEnd;		
		}

		if(pos.x > start.x && pos.x < end.x)
			if(pos.y > start.y && pos.y < end.y)
				return true;
		return false;

	}
	private void action(int team, Player player, AbstractBall ball) {
		
		//serve
		if(serve){
			player.serve(ball);
			serve = false;
			return;
		}
		
		if(team == Team.T1) {
			//attack
			if(player.getHitTarget().x > PitchConstants.half)
				player.attack(ball);
			//pass
			else
				player.pass(ball);
		}
		else if(team == Team.T2) {
			//attack
			if(player.getHitTarget().x <  PitchConstants.half)
				player.attack(ball);
			//pass
			else
				player.pass(ball);
		}
		
		
	}
	@Override
	protected void stop() {
		pitch.setPhase(Phases.TACTICAL);
	}
}
